package com.bmmtwabp.employeedashbord.util;

import com.alibaba.fastjson.JSONObject;
import com.bmmtwabp.employeedashbord.domain.Heat;
import com.bmmtwabp.employeedashbord.vo.AccessTokenVo;
import java.util.*;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 常量类
 * @Param:
 * @return:
 * @Author: fenghouzhi
 * @Date: 2018/8/28
 */
@Slf4j
public class CodeUtil {

  /**
   * 初始化区域数组
   */
  public static final String[] ZOOM_ARRAY = new String[]{"北区", "南区"};

  /**
   * 小程序AppId
   */
  public static final String APP_ID = "wx56da2b4ba3842c58";

  /**
   * 小程序AppSecret
   */
  public static final String APP_SECRET = "739b3fbed3e8767f57599801d338ea3b";

  /**
   * 微信小程序登录URL
   */
  public static final String WEIXIN_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";

  /**
   * 微信小程序请求接口Token URL
   */
  public static final String WEIXIN_TOKEN = "https://api.weixin.qq.com/cgi-bin/token";

  public static final String AUTHORIZATION_CODE = "authorization_code";

  public static final String CLIENT_CREDENTIAL = "client_credential";

  /**
   * 获取模板消息URL
   */
  public static final String WEIXIN_TEMPLATE_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";

  /**
   * 模板消息Id-[用户热饭到号]
   */
  public static final String TEMPLATE_ID = "62IWNFugwb278B9aOUrJGybRPUwT80sSrhjFgSgSPXM";

  /**
   * online-用户热饭时长限定--10分钟
   * dev----用户热饭时长限定--30秒
   */
  //public static final Integer HEAT_TIME = 10 * 60 * 1000;
  public static final Integer HEAT_TIME = 30 * 1000;

  /**
   * online-用户准备热饭的最大时间限制--60秒
   * dev----用户准备热饭的最大时间限制--15秒
   */
  //public static final Integer HEAT_WAIT_MAX_TIME = 60 * 1000;
  public static final Integer HEAT_WAIT_MAX_TIME = 15 * 1000;

  /**
   * 微波炉启用
   */
  public static final Integer MICROWAVE_USED = 1;

  /**
   * 微波炉未使用
   */
  public static final Integer MICROWAVE_NOTUSED = 0;

  /**
   * 设置全局-保存当前准备热饭的集合Map
   */
  public static Map<String, Heat> WAIT_HEAT_MAP = new HashMap<>(2);

  /**
   * 全局静态access_token对象
   */
  public static AccessTokenVo ACCESSTOKENVO = null;

  /**
   * 获取access_token对象
   */
  public static String getAccesstoken() {

    Map<String, Object> paramMap = new HashMap<>(3);
    paramMap.put("grant_type", CLIENT_CREDENTIAL);
    paramMap.put("appid", APP_ID);
    paramMap.put("secret", APP_SECRET);

    try {
      String responseData = HttpRequestUtil.httpGet(WEIXIN_TOKEN, paramMap);
      AccessTokenVo tokenVo = (AccessTokenVo) JSONObject
          .parseObject(responseData, AccessTokenVo.class);
      if (tokenVo != null) {
        ACCESSTOKENVO = tokenVo;
        int currentTime = DateUtil.getSecondTimestamp(new Date());
        ACCESSTOKENVO.setExpires_in(currentTime + ACCESSTOKENVO.getExpires_in());
      }
      log.info("AccessTokenVo:     " + ACCESSTOKENVO);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return ACCESSTOKENVO.getAccess_token();
  }

  /**
   * access_token过期检测
   */
  public static String overdueDetection() {
    int currentTime = DateUtil.getSecondTimestamp(new Date());
    if (currentTime > ACCESSTOKENVO.getExpires_in()) {
      log.info("access_token过期，重新请求!");
      return getAccesstoken();
    } else {
      log.info("access_token还未过期，继续使用!");
      return ACCESSTOKENVO.getAccess_token();
    }
  }

}